function msg = convdec(y, trel)
  y = y(:) 
  codeLen = length(y) 
  regNum = log2(trel.numStates)
  msgLen = codeLen - regNum
  numStates = trel.numStates 
  d = zeros(numStates, 1) 
  preStates = zeros(numStates, codeLen)
  bitOutputSymbols = log2(trel.numOutputSymbols)
    
  curStates = 0;
  msbWeight = 2^(regNum-1);
  for i = 1:regNum
    tempd = d+%inf
    for cur = curStates
      for msgEvl = 0:trel.numInputSymbols - 1
        nextState = trel.nextStates(cur+1, msgEvl+1)
        bitDiff = length(find(de2bi(trel.outputs(cur+1,msgEvl+1),bitOutputSymbols) ~= de2bi( y(i),bitOutputSymbols)))
        if tempd(nextState+1) > d(cur+1) + bitDiff
          tempd(nextState+1) = d(cur+1) + bitDiff
          preStates(nextState+1, i) = cur 
        end
        
      end
    end
    d = tempd
    curStates = curStates/2;
    curStates = [curStates curStates+msbWeight];
  end
  
  for i = (regNum+1):(codeLen - regNum)
    tempd = d+%inf
    for cur = 0:numStates-1
      for msgEvl = 0:trel.numInputSymbols - 1
        nextState = trel.nextStates(cur+1, msgEvl+1)
        bitDiff = length(find(de2bi(trel.outputs(cur+1,msgEvl+1),bitOutputSymbols) ~= de2bi( y(i),bitOutputSymbols)))
        if tempd(nextState+1) > d(cur+1) + bitDiff
          tempd(nextState+1) = d(cur+1) + bitDiff
          preStates(nextState+1, i) = cur 
        end
        
      end
    end
    d = tempd 
  end

  availStates = numStates
  for i = (codeLen - regNum+1):codeLen
    tempd = d+%inf
    for cur = 0:availStates-1
      msgEvl = 0 
      nextState = trel.nextStates(cur+1, msgEvl+1)
      bitDiff = length(find(de2bi(trel.outputs(cur+1,msgEvl+1),bitOutputSymbols) ~= de2bi( y(i),bitOutputSymbols)))
      if tempd(nextState+1) > d(cur+1) + bitDiff
        tempd(nextState+1) = d(cur+1) + bitDiff
        preStates(nextState+1, i) = cur 
      end
    end
    d = tempd 
    availStates = availStates/2 
  end
  
  cur = 0 
  for i = codeLen:-1:1
    msg(i) = int(cur/msbWeight) 
    cur = preStates(cur+1, i)
  end
  msg = msg(1:msgLen)
endfunction
